<?php
// +----------------------------------------------------------------------
// | YBlog
// +----------------------------------------------------------------------
// | Copyright (c) 2008 http://www.yhustc.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: yhustc <yhustc@gmail.com>
// +----------------------------------------------------------------------
// $Id$

/**
 +------------------------------------------------------------------------------
 * YBlog日志类别与标签管理模块
 +------------------------------------------------------------------------------
 * @author    yhustc <yhustc@gmail.com>
 * @version   $Id$
 +------------------------------------------------------------------------------
 */


class Cate_tagAction extends BaseAction
{
	/**
     +----------------------------------------------------------
     * 分类/Tag列表
     +----------------------------------------------------------
     * @access public 
     +----------------------------------------------------------
     */
	function index()
	{
		$categoryDao = D("Category");
		$categoryList = $categoryDao->findAll();
		
		if($this->options["use_balanceTags"]) //使用大小一样的tag云图
		{
			$tagDao = D("Tags");
			$tagList = $tagDao->findAll();
		}
		else
		{
			$tagDao = new Model();
			$tagList = $tagDao->query("SELECT COUNT(postid) AS num,tag_name FROM `".C("DB_PREFIX")."tags` GROUP BY tag_name ORDER BY num DESC");
			
			$counts = array();
			foreach($tagList as $tag)
				$counts[$tag["tag_name"]] = $tag["num"];
			$minCout = min($counts);
			$spread = max($counts) - $min_count;
			if ( $spread <= 0 )
				$spread = 1;
			$font_spread = 14;
			$font_step = $font_spread / $spread;
			
			foreach($tagList as $key=>$tag)
				$tagList[$key]["size"] = (8 + (($tag["num"] - $minCout) * $font_step));
		}
		
		$this->assign("categoryList",$categoryList);
		$this->assign("tagList",$tagList);
		$this->assign("categoryArray",$categoryDao->getCols($categoryList,"cid,name"));
		$this->assign("pageFile","Cate_tag:index");
		$this->layout();
	}
	
	/**
     +----------------------------------------------------------
     * 保存对分类的添加/修改
     +----------------------------------------------------------
     * @access public 
     +----------------------------------------------------------
     */
	function editCategory()
	{
		$categoryDao = D("Category");
		$rs = $categoryDao->create();
		if(!$rs)
			$this->error($categoryDao->getError());
			
		if(isset($_POST["cid"]) && is_numeric($_POST["cid"]))
		{
			$result = $categoryDao->save();
			$successMsg = "更新分类信息成功";
			$errorMsg = "更新分类信息失败";
		}
		else
		{
			$result = $categoryDao->add();
			$successMsg = "添加分类信息成功";
			$errorMsg = "添加分类信息失败";
		}
		
		if($result)
			$this->success($successMsg);
		else
			$this->error($errorMsg);
	}
	
	/**
     +----------------------------------------------------------
     * 删除指定分类
     +----------------------------------------------------------
     * @access public 
     +----------------------------------------------------------
     */
	function delCategory()
	{
		if(isset($_GET["cid"]) && is_numeric($_GET["cid"]))
		{
			$categoryDao = D("Category");
			$categoryDao->delete("cid='".$_GET["cid"]."'");
	
			$postDao = D("Posts");
			$postDao->delete("post_category='".$_GET["cid"]."'");
	
			$this->assign("jumpUrl",url("index"));
			$this->success("删除分类成功");
		}
		else
			$this->error("错误的参数");
	}
	
	/**
     +----------------------------------------------------------
     * 移动分类
     +----------------------------------------------------------
     * @access public 
     +----------------------------------------------------------
     */
	function moveCategory()
	{
		$postDao = D("Posts");
		$result = $postDao->setField("post_category",$_POST["newCid"],"post_category='".$_POST["originCid"]."'");

		if($result)
		{
			//重建分类的计数
			$postDao->execute("UPDATE `".C("DB_PREFIX")."category` c SET count = ( SELECT COUNT( `ID` ) FROM `".C("DB_PREFIX")."posts` p WHERE c.`cid` = p.`post_category` AND p.`post_type`='post')");
			$this->success("移动分类成功");
		}
		else
			$this->error("移动分类失败");
	}
	
	/**
     +----------------------------------------------------------
     * 删除指定Tag
     +----------------------------------------------------------
     * @access public 
     +----------------------------------------------------------
     */
	function delTag()
	{
		$tagDao = D("Tags");
		$rs = $tagDao->delete("tag_name='".urldecode($_GET["tag_name"])."'");
		
		//delete返回的是成功删除的记录数
		//第一个delete肯定是有结果的,而第二个delete可能由于这个tag没本关联的日志(日志tag可以修改的)而返回0
		if($rs)
			$this->success("删除tag成功");
		else
			$this->error("删除tag失败");
	}
}